package com.jt.www.remote;

import config.FeignConfiguration;
import feign.hystrix.FallbackFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@FeignClient(value = "idg-svc", url = "${remote.idg_svc_url}",
        configuration = FeignConfiguration.class,
        fallbackFactory = IdgClient.FeignClientFallbackFactory.class)
public interface IdgClient {

    @RequestMapping(method = RequestMethod.GET, value = "/api/v1/idg", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    long nextId();

    @RequestMapping(method = RequestMethod.GET, value = "/api/v1/resub_token", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    String getToken();

    @Component
    class FeignClientFallbackFactory implements FallbackFactory<IdgClient> {

        public static final Logger log = LoggerFactory.getLogger(FeignClientFallbackFactory.class);

        @Override
        public IdgClient create(Throwable cause) {
            return new IdgClient() {

                @Override
                public long nextId() {
                    log.error("调用保单中心查询出单状态失败", cause);
                    return -1L;
                }

                @Override
                public String getToken() {
                    log.error("获取token失败", cause);
                    return null;
                }
            };
        }
    }
}
